package com.express.controller;


import com.express.auto.JSONAuthentication;
import com.express.entity.ExCar;
import com.express.entity.ExUser;
import com.express.mapper.ExUserMapper;
import com.express.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author express
 * @since 2022-01-22
 */
@RestController
@RequestMapping("/ex-user")
public class ExUserController extends JSONAuthentication {
    @Autowired
   private  ExUserMapper exUserMapper;

    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody ExUser exUser) throws  Exception{

        String password= new BCryptPasswordEncoder().encode(exUser.getUserPassword());
        exUser.setUserPassword(password);
        System.out.println(password);
        int i=exUserMapper.insert(exUser);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=exUserMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改用户")
    @PostMapping("/upd")
    public void updUser(HttpServletRequest request, HttpServletResponse response, @RequestBody ExUser exUser)throws Exception{
        int i=exUserMapper.updateById(exUser);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询用户")
    @PostMapping("/look")
    public void lookUser(HttpServletRequest request, HttpServletResponse response)throws  Exception{
        List<ExUser> list =exUserMapper.findAll();
            Result ok = Result.ok().data("user",list);
            this.WriteJSON(request, response, ok);

    }

}

